/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.management.user_account;

import tcp_net.management.user_account.debug.DebugInfo;
import java.util.List;
import java.util.ArrayList;

/**
	This class provides to store email user account data
	@author J. Okuno
*/
public class UserAccount
{
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.USER_ACCOUNT_DEBUG;
	
	/**
		A user name
	*/
	private String userName;
	
	/**
		A user's password
	*/
	private String password;
	
	/**
		true if a user account is to be enabled
	*/
	private boolean enabled;
	
	/**
		A list of email messages
	*/
	private List<Message> messages;
	
	/**
		Init UserAccount class (Restored user account)
		@param name a user name
		@param pass a user's password
		@param en true if a user account is enabled
	*/
	public UserAccount(String name, String pass, boolean en) 
	{
		userName = name;
		password = pass;
		
		enabled = en;
		messages = new ArrayList<Message>();
		
		DebugInfo.print(DEBUG,"UserAccount name: "+userName+" password: "+ password +" created.");
	}
	
	/**
		Init UserAccount class by default a user account is enabled (New user account)
		@param name a user name
		@param pass a user's password
	*/
	public UserAccount(String name, String pass)
	{
		this(name, pass, true);
	}
	
	/**
		This method adds a new email message to the message list
		@param msg a new email message
	*/
	public void addMessage(String msg) 
	{ 
		DebugInfo.print(DEBUG,"Message: "+msg+" is added");
		messages.add(new Message(userName, msg)); 
	}
	
	/**
		This method provides a list of email messages from a user's mailbox
		@return a list of email messages
	*/
	public List<Message> getMessages() 
	{ 
		DebugInfo.print(DEBUG,"List of messages are requested");
		return messages; 
	}
	
	/**
		This method provides a list of not delete marked email messages from a user's mailbox
		@return a list of not delete marked email messages
	*/
	public List<Message> getMessagesNotDeleted()
	{
		List<Message> msgs_not_del = new ArrayList<Message>();
		for(Message m : messages)
		{
			if(!m.isDeleted()) { msgs_not_del.add(m); }
		}
		
		DebugInfo.print(DEBUG,"List of not deleted marked messages are requested");
		return msgs_not_del;
	}
	
	/**
		This method removes delete messages
	*/
	public void removeDeleteMessages()
	{
		DebugInfo.print(DEBUG,"Delete marked messages removed");
		messages = getMessagesNotDeleted();
	}
	
	/**
		This method calculates a number of messages in a mailbox (exc. delete marked messages)
		@return a number of messages in a mailbox (exc. delete marked messages)
	*/
	public int countNoOfMessages()
	{
		return getMessagesNotDeleted().size();
	}
	
	/**
		This method provides a total sum of user's messages
		@return a total sum of user's messages
	*/
	public int getAllMessageSize()
	{
		int size = 0;
		
		for(Message m : getMessagesNotDeleted()) 
		{
			size += m.getMsgSize();
		}
		
		return size;
		
	}
	
	/**
		This method enables a user account
	*/
	public void enable() 
	{ 
		enabled = true; 
		DebugInfo.print(DEBUG,"UserAccount name: "+userName+" enabled.");
	}
	
	/**
		This method disables a user account
	*/
	public void disable() 
	{ 
		enabled = false; 
		DebugInfo.print(DEBUG,"UserAccount name: "+userName+" disabled.");
	}
	
	/**
		This method resets a user name and a password
	*/
	public void reset()
	{	
		userName = "";
		password = "";
		
		enabled = false;
		DebugInfo.print(DEBUG,"UserAccount name: "+userName+" resetted.");
	}

	/**
		This method sets a user's name
		@param name a user name
	*/
	public void setUserName(String name) 
	{ 
		DebugInfo.print(DEBUG,"UserAccount id: name: "+userName+"\'s name changed to: "+name);
		userName = name; 
	}
	
	/**
		This method sets a user password
		@param pass a password
	*/
	public void setPassword(String pass) 
	{ 
		password = pass; 
		DebugInfo.print(DEBUG,"UserAccount id: name: "+userName+"\' password changed to: "+pass);
	}
	
	/**
		This method provides a user's name
		@return a user name
	*/
	public String getUserName() 
	{ 
		DebugInfo.print(DEBUG,"User Name requested: "+userName);
		return userName; 
	}
	
	/**
		This method provides a user's password
		@return a user's password
	*/
	public String getPassword() 
	{ 
		DebugInfo.print(DEBUG,"Password requested: "+password);
		return password; 
	}
	
	/**
		This method tests if a user is enabled
		@return true if a user is enabled
	*/
	public boolean isEnabled() 
	{ 
		DebugInfo.print(DEBUG,"User "+userName+" is enabled: "+enabled);
		return enabled; 
	}
}